﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using LeandroPrado.FluentNHibernate.Domain.Entidades;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Cfg;

namespace LeandroPrado.FluentNHibernate.Domain.Infra
{
    public class Conexao
    {
        public static ISessionFactory CreateSessionFactory()
        {
            FluentConfiguration configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                            x => x.FromConnectionStringWithKey("ConexaoBanco")).ShowSql())
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Pessoa>());

            return configuration.BuildSessionFactory();
        }

        public static void CriarTabelasBanco()
        {
            FluentConfiguration configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                            x => x.FromConnectionStringWithKey("ConexaoBanco")).ShowSql())
                .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true,true))
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Pessoa>());

            configuration.BuildSessionFactory();
        }

        public static void CriarScriptsBanco()
        {
            FluentConfiguration configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                            x => x.FromConnectionStringWithKey("ConexaoBanco")).ShowSql())
                .ExposeConfiguration(cfg => new SchemaExport(cfg).SetOutputFile("C:\\Temp\\scripts.sql").Create(true, false))
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Pessoa>());

            configuration.BuildSessionFactory();
        }

        public static void CriarArquivosHbm()
        {
            FluentConfiguration configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                            x => x.FromConnectionStringWithKey("ConexaoBanco")).ShowSql())
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<Pessoa>().ExportTo("C:\\Temp"));

            configuration.BuildSessionFactory();
        }
    }
}